﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;
using CSSoft.VNTower.Domain;
using Microsoft.SharePoint.WebControls;
using Telerik.Web.UI;
using CSSoft;
using System.Collections;
using System.Net.Mail;
using System.Text;

namespace CSSoft.VNTower.WeeklyReport
{
    public partial class WeeklyReportUserControl : UserControl
    {
        private WeeklyReportItem _data;
        public WeeklyReportItem Data {
            get { 
                if (ViewState["WeeklyReportItem"] == null) 
                    ViewState["WeeklyReportItem"] = new WeeklyReportItem();
                _data = (WeeklyReportItem)ViewState["WeeklyReportItem"]; 
                return _data; 
            }
            set{
                ViewState["WeeklyReportItem"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                #region Read data
                WeeklyReportItem data = new WeeklyReportItem();
                //Load Daily Report
                bool addNew = true;
                CS2Week week = new CS2Week(CS2Week.CalculateLastDateOfWeek(DateTime.Today));
                GeneralListData(week);
                if (Request.QueryString["_Id"] != null) //ReportID
                {
                    try
                    {
                        int reportId = CS2Convert.ToInt(Request.QueryString["_Id"]);
                        SPList listWeeklyReport = CS2Web.GetList(Constants.LIST_WEEKLY_REPORT);
                        SPListItem itemWeeklyReport = listWeeklyReport.GetItemById(reportId);
                        data.GetItem(itemWeeklyReport);
                        week = new CS2Week(CS2Week.CalculateLastDateOfWeek(data.Week,data.Year));
                        addNew = false;
                    }
                    catch { }
                }
                else
                {
                    SPList list = CS2Web.GetList(Constants.LIST_WEEKLY_REPORT);
                    SPQuery query = new SPQuery();
                    query.Query = String.Format(@"<Where>
                        <And>
                            <And>
                                 <Eq>
                                    <FieldRef Name='Year' />
                                    <Value Type='Text'>{0}</Value>
                                 </Eq>
                                 <Eq>
                                    <FieldRef Name='Week' />
                                    <Value Type='Number'>{1}</Value>
                                 </Eq>
                              </And>
	                    <Eq>
		                    <FieldRef Name='Author' LookupId='TRUE'/>
		                    <Value Type='User'>{2}</Value>
	                    </Eq>
                        </And>
                    </Where>", week.Year, week.WeekNumber, CS2Web.CurrentUser.ID);
                    SPListItemCollection items = list.GetItems(query);
                    if (CS2SPQueryBuilder.HasItems(items))
                    {
                        data.GetItem(items[0]);
                        addNew = false;
                    }
                }
                if (addNew)
                {
                    data.Week = week.WeekNumber;
                    data.Year = week.FirstDateOfWeek.Year;
                    data.From = week.FirstDateOfWeek;
                    data.Till = week.LastDateOfWeek;
                    data.Reporter = new UserItem { User = CS2Web.CurrentUser };
                }
                UpdateWeekData(week);
                LoadDataReport(data);
                #endregion

                #region Read Permission
                //Reporter Controls
                if (CS2Web.CurrentUser.ID != data.Reporter.Id)
                {
                    EmailTo.Enabled = false;
                    EmailCC.Enabled = false;
                    Note.Enabled = false;
                    //Files grid
                    SPList listProjects = CS2Web.GetList(Constants.LIST_PROJECTS);
                    SPQuery queryProjects = new SPQuery();
                    queryProjects.Query = String.Format(@"<Where>
	                        <Eq>
		                        <FieldRef Name='Members' LookupId='TRUE'/>
		                        <Value Type='User'>{0}</Value>
	                        </Eq>
                        </Where>", CS2Web.CurrentUser.ID);

                    SPListItemCollection itemProjects = listProjects.GetItems(queryProjects);
                    if (!CS2SPQueryBuilder.HasItems(itemProjects))
                    {
                        ReportTasksAssigned.Enabled = false;
                        ReportTasksAssigned.Columns[0].Visible = false; //Edit
                        Submit.Visible = false;
                    }
                }
                //Manager control
                SPList listFunctions = CS2Web.GetList(Constants.LIST_FUNCTIONS);
                SPQuery queryFunction = new SPQuery();
                queryFunction.Query = String.Format(@"<Where>
	                    <Eq>
		                    <FieldRef Name='Members' LookupId='TRUE'/>
		                    <Value Type='User'>{0}</Value>
	                    </Eq>
                    </Where>", data.Reporter.Id);
                SPListItemCollection itemFunctions = listFunctions.GetItems(queryFunction);
                if (CS2SPQueryBuilder.HasItems(itemFunctions))
                {
                    SPFieldUserValueCollection lineManagers = CS2Convert.ToSPFieldUserValueCollection(itemFunctions[0]["Manager"]);
                    foreach (SPFieldUserValue manager in lineManagers)
                    {
                        if (manager.LookupId == CS2Web.CurrentUser.ID)
                        {
                            LineManagerComment.Enabled = true;
                            Approved.Visible = true;
                            Rejected.Visible = true;
                            break;
                        }
                    }
                }
                #endregion
            }
        }

        private void GeneralListData(CS2Week week)
        {
            for (int i = 1; i <= 53; i++)
                Week.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            for (int i = -10; i <= 10; i++)
            {
                string yearValue = (i + week.Year).ToString();
                Year.Items.Add(new RadComboBoxItem(yearValue, yearValue));
            }
        }

        private void UpdateWeekData(CS2Week week)
        {
            Week.SelectedValue = week.WeekNumber.ToString();
            if (Year.Items.FindItemByValue(week.Year.ToString()) == null)
                Year.Items.Add(new RadComboBoxItem(week.Year.ToString(), week.Year.ToString()));
            Year.SelectedValue = week.Year.ToString();
            From.SelectedDate = week.FirstDateOfWeek;
            Till.SelectedDate = week.LastDateOfWeek;
        }

        private void LoadDataReport(WeeklyReportItem data)
        {
            //Get report files
            try
            {
                SPList listWeeklyReportTasks = CS2Web.GetList(Constants.LIST_WEEKLY_REPORT_TASKS);
                SPQuery queryWeeklyReportTasks = new SPQuery();
                queryWeeklyReportTasks.Query = String.Format(@"<Where>
                      <And>
	                     <And>
                             <Eq>
                                <FieldRef Name='Year' />
                                <Value Type='Text'>{0}</Value>
                             </Eq>
                             <Eq>
                                <FieldRef Name='Week' />
                                <Value Type='Number'>{1}</Value>
                             </Eq>
                         </And>
	                     <Eq>
		                    <FieldRef Name='Author' LookupId='TRUE'/>
		                    <Value Type='User'>{2}</Value>
	                     </Eq>
                      </And>
                    </Where>", data.Year, data.Week, data.Reporter.Id);
                SPListItemCollection itemWeeklyReportTasks = listWeeklyReportTasks.GetItems(queryWeeklyReportTasks);
                if (CS2SPQueryBuilder.HasItems(itemWeeklyReportTasks))
                {
                    foreach (SPListItem item in itemWeeklyReportTasks)
                        data.TasksAssigned.Add(new UserTaskItem().ReadItem(item));
                }
                else
                {
                    SPList listEmployeeTasks = CS2Web.GetList(Constants.LIST_EMPLOYEE_TASKS);
                    SPList listProjects = CS2Web.GetList(Constants.LIST_PROJECTS);
                    SPQuery queryEmployeeTasks = new SPQuery();
                    queryEmployeeTasks.Query = String.Format(@"<Where>
                        <And>
                            <Neq>
                            <FieldRef Name='PercentComplete' />
                            <Value Type='Number'>1</Value>
                            </Neq>
                            <Eq>
                            <FieldRef Name='AssignedTo' LookupId='TRUE'/>
		                    <Value Type='User'>{0}</Value>
                            </Eq>
                        </And>
                    </Where>", data.Reporter.Id);
                    SPListItemCollection itemEmployeeTasks = listEmployeeTasks.GetItems(queryEmployeeTasks);
                    if (CS2SPQueryBuilder.HasItems(itemEmployeeTasks))
                        foreach (SPListItem item in itemEmployeeTasks)
                            data.TasksAssigned.Add(new UserTaskItem().ReadTask(item));
                }
            }
            catch { }
            Data = data;

            BindUserView();
            BindReportData();
        }

        private void BindReportData()
        {
            EmailTo.UpdateEntities(UserItem.GetEntitiesArrayList(Data.ReportTo));
            EmailCC.UpdateEntities(UserItem.GetEntitiesArrayList(Data.ReportCC));
            Note.Text = Data.Note;
            LineManagerComment.Text = Data.LineManagerComment;
            Status.Text = Data.ReportStatus;
            ReportTasksAssigned.Rebind();
            ActionRegister.Rebind();
        }

        private void BindUserView()
        {
            Name.Text = Data.Reporter.Title;
            Code.Text = Data.Reporter.Code;
            Title.Text = Data.Reporter.JobTitle;
            Division.Text = Data.Reporter.Department;
            Region.Text = Data.Reporter.Region;
        }

        private void LoadDataReport(CS2Week week)
        {
            WeeklyReportItem data = new WeeklyReportItem();
            bool addNew = true;
            try
            {
                SPList list = CS2Web.GetList(Constants.LIST_WEEKLY_REPORT);
                SPQuery query = new SPQuery();
                query.Query = String.Format(@"<Where>
                        <And>
                            <And>
                                 <Eq>
                                    <FieldRef Name='Title' />
                                    <Value Type='Text'>{0}</Value>
                                 </Eq>
                                 <Eq>
                                    <FieldRef Name='Week' />
                                    <Value Type='Number'>{1}</Value>
                                 </Eq>
                            </And>
	                    <Eq>
		                    <FieldRef Name='Author' LookupId='TRUE'/>
		                    <Value Type='User'>{2}</Value>
	                    </Eq>
                        </And>
                    </Where>", week.Year, week.WeekNumber, Data.Reporter.Id);
                SPListItemCollection items = list.GetItems(query);
                if (CS2SPQueryBuilder.HasItems(items))
                {
                    data.GetItem(items[0]);
                    addNew = false;
                }
            }
            catch { }
            if(addNew)
            {
                data.Week = week.WeekNumber;
                data.Year = week.FirstDateOfWeek.Year;
                data.From = week.FirstDateOfWeek;
                data.Till = week.LastDateOfWeek;
                data.Reporter = new UserItem { User = CS2Web.CurrentUser };
            }
            LoadDataReport(data);
        }

        protected void Week_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            CS2Week week = new CS2Week(CS2Week.CalculateLastDateOfWeek(CS2Convert.ToInt(Week.SelectedValue), CS2Convert.ToInt(Year.SelectedValue)));
            UpdateWeekData(week);
            LoadDataReport(week);
        }

        protected void Year_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            CS2Week week = new CS2Week(CS2Week.CalculateLastDateOfWeek(CS2Convert.ToInt(Week.SelectedValue), CS2Convert.ToInt(Year.SelectedValue)));
            UpdateWeekData(week);
            LoadDataReport(week);
        }

        protected void From_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            if (From.SelectedDate != null)
            {
                CS2Week week = new CS2Week(CS2Week.CalculateLastDateOfWeek(From.SelectedDate.Value));
                UpdateWeekData(week);
                LoadDataReport(week);
            }
        }

        protected void Till_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            if (Till.SelectedDate != null)
            {
                CS2Week week = new CS2Week(CS2Week.CalculateLastDateOfWeek(Till.SelectedDate.Value));
                UpdateWeekData(week);
                LoadDataReport(week);
            }
        }

        protected void ReportTasksAssigned_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            ReportTasksAssigned.DataSource = Data.TasksAssigned;
        }

        protected void ActionRegister_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            ActionRegister.DataSource = Data.ActionRegister;
        }

        protected void ReportTasksAssigned_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            Hashtable rowItem = new Hashtable();
            ((GridEditableItem)e.Item).ExtractValues(rowItem);
            int id = CS2Convert.ToInt(rowItem["Id"]);
            UserTaskItem userTask = Data.TasksAssigned.First(i => i.Id == id);
            userTask.ReasonDelay = CS2Convert.ToString(rowItem["ReasonDelay"]);
            userTask.Delay = CS2Convert.ToBoolean(rowItem["Delay"]);
            userTask.PercentComplete = CS2Convert.ToDecimal(CS2Convert.ToString(rowItem["PercentComplete"]));
            userTask.FS = CS2Convert.ToDateTime(rowItem["FS"]);
            userTask.FF = CS2Convert.ToDateTime(rowItem["FF"]);
            userTask.AS = CS2Convert.ToDateTime(rowItem["AS"]);
            userTask.AF = CS2Convert.ToDateTime(rowItem["AF"]);
            userTask.PMAproved = CS2Convert.ToString(rowItem["PMAproved"]);
            userTask.PMComment = CS2Convert.ToString(rowItem["PMComment"]);
            ActionRegister.Rebind();
        }

        protected void ActionRegister_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            Hashtable rowItem = new Hashtable();
            ((GridEditableItem)e.Item).ExtractValues(rowItem);
            int id = CS2Convert.ToInt(rowItem["Id"]);
            UserTaskItem userTask = Data.TasksAssigned.First(i => i.Id == id);
            userTask.ActionRegister = CS2Convert.ToString(rowItem["ActionRegister"]);
            userTask.PlannedFinish = CS2Convert.ToDateTime(rowItem["PlannedFinish"]);
            userTask.WorkingPlace = CS2Convert.ToString(rowItem["WorkingPlace"]);
            userTask.Note = CS2Convert.ToString(rowItem["Note"]);
            ActionRegister.Rebind();
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            try
            {
                //Save data
                CS2Week week = new CS2Week(CS2Week.CalculateLastDateOfWeek(CS2Convert.ToInt(Week.SelectedValue), CS2Convert.ToInt(Year.SelectedValue)));
                CS2Week currentWeek = new CS2Week(CS2Week.CalculateLastDateOfWeek(DateTime.Today));
                //Update Weekly Report
                SPList list = CS2Web.GetList(Constants.LIST_WEEKLY_REPORT);
                SPQuery query = new SPQuery();
                query.Query = String.Format(@"<Where>
                        <And>
                            <And>
                                 <Eq>
                                    <FieldRef Name='Year' />
                                    <Value Type='Text'>{0}</Value>
                                 </Eq>
                                 <Eq>
                                    <FieldRef Name='Week' />
                                    <Value Type='Number'>{1}</Value>
                                 </Eq>
                            </And>
	                    <Eq>
		                    <FieldRef Name='Author' LookupId='TRUE'/>
		                    <Value Type='User'>{2}</Value>
	                    </Eq>
                        </And>
                    </Where>", week.Year, week.WeekNumber, Data.Reporter.Id);
                SPListItemCollection items = list.GetItems(query);
                SPListItem item;
                if (CS2SPQueryBuilder.HasItems(items))
                    item = items[0];
                else
                {
                    item = list.AddItem();
                    item["Title"] = String.Format("{0}-{1}", week.WeekNumber, week.Year);
                    item["Year"] = week.Year;
                    item["Week"] = week.WeekNumber;
                    item["From"] = week.FirstDateOfWeek;
                    item["Till"] = week.LastDateOfWeek;
                }
                item["Note"] = Note.Text;
                //item["LineManagerComment"] = LineManagerComment.Text;
                //item["Status"] = ReportStatus.SelectedValue;
                SPFieldUserValueCollection reportTo = new SPFieldUserValueCollection();
                SPFieldUserValueCollection reportCC = new SPFieldUserValueCollection();
                MailAddressCollection To = new MailAddressCollection();
                MailAddressCollection CC = new MailAddressCollection();
                foreach (PickerEntity entity in EmailTo.ResolvedEntities)
                {
                    SPFieldUserValue fuv = new SPFieldUserValue(CS2Web.CurrentWeb, Convert.ToInt16(entity.EntityData[PeopleEditorEntityDataKeys.UserId]), entity.Description);
                    reportTo.Add(fuv);
                    To.Add(new MailAddress(fuv.User.Email, fuv.User.Name));
                }
                foreach (PickerEntity entity in EmailCC.ResolvedEntities)
                {
                    SPFieldUserValue fuv = new SPFieldUserValue(CS2Web.CurrentWeb, Convert.ToInt16(entity.EntityData[PeopleEditorEntityDataKeys.UserId]), entity.Description);
                    reportCC.Add(fuv);
                    CC.Add(new MailAddress(fuv.User.Email, fuv.User.Name));
                }
                item["ReportTo"] = reportTo;
                item["ReportCC"] = reportCC;
                item.SystemUpdate();
                Data.Id = item.ID;
                //Update Weekly Report Tasks
                SPList listWeeklyReportTasks = CS2Web.GetList(Constants.LIST_WEEKLY_REPORT_TASKS);
                SPList listEmployeeTasks = CS2Web.GetList(Constants.LIST_EMPLOYEE_TASKS);
                foreach (UserTaskItem task in Data.TasksAssigned)
                {
                    SPListItem itemTask;
                    if (task.Id > 0)
                        itemTask = listWeeklyReportTasks.GetItemById(task.Id);
                    else
                    {
                        itemTask = listWeeklyReportTasks.AddItem();
                        itemTask["Title"] = task.Title;
                        itemTask["Year"] = week.Year;
                        itemTask["Week"] = week.WeekNumber;
                        if (task.Project.Id != 0)
                            itemTask["Project"] = task.Project.LookupValue;
                        if (task.SourceTask.Id > 0)
                            itemTask["SourceTask"] = task.SourceTask.LookupValue;
                    }
                    itemTask["Delay"] = task.Delay;
                    itemTask["ReasonDelay"] = task.ReasonDelay;
                    decimal percentComplete = task.PercentComplete / 100;
                    itemTask["PercentComplete"] = percentComplete;
                    itemTask["Start"] = task.FS;
                    itemTask["Finish"] = task.FF;
                    itemTask["ActualStart"] = task.AS;
                    itemTask["ActualFinish"] = task.AF;
                    itemTask["ManagerApproved"] = task.PMAprovedCheckBox ? "Approved" : "Rejected";
                    itemTask["ManagerJudgerment"] = task.PMComment;
                    itemTask["ActionRegister"] = task.ActionRegister;
                    itemTask["PlannedFinish"] = task.PlannedFinish;
                    itemTask["WorkingPlace"] = task.WorkingPlace;
                    itemTask["Note"] = task.Note;
                    itemTask.SystemUpdate();
                    //Update percentComplete of SourceTask only for Current Week
                    if (task.SourceTask.Id > 0 && currentWeek.LastDateOfWeek <= week.LastDateOfWeek)
                    {
                        SPListItem itemSourceTask = listEmployeeTasks.GetItemById(task.SourceTask.Id);
                        itemSourceTask["PercentComplete"] = percentComplete;
                        itemSourceTask.SystemUpdate();
                    }
                }
                SubmitMessage.Text = "<br/>Hoàn thành báo cáo!<br/>Report completed!";
                SendMail(To, CC, "submited");              
            }
            catch
            {
                SubmitMessage.Text = "<br/>Không thể lưu báo cáo, vui lòng thử lại!<br/>Problem save report, please try again!";
            }
        }

        private void SendMail(MailAddressCollection To, MailAddressCollection CC, string action)
        {
            string subject = String.Format("[Weekly Report] Report week '{0}' {1}", Data.Week, action);
            StringBuilder body = new StringBuilder();
            body.AppendFormat("<b>Reporter: </b>{0}<br/>", Data.Reporter.Title);
            body.AppendFormat("<b>Node: </b><br/>{0}<br/>", Data.Note);
            body.AppendFormat("<b>Updated by: </b>{0}<br/>", CS2Web.CurrentUser.Name);
            string url = Request.Url.AbsoluteUri;
            if (url.Contains('?')) url = url.Split('?')[0];
            body.AppendFormat("<b>Link: </b><a href='{0}?_Id={1}'>{0}?_Id={1}</a>", url.Replace(" ", "%20"), Data.Id);
            bool sendMailCompleted = CS2Web.SendEmailAttachments(To, CC, new MailAddressCollection(), subject, body.ToString());
            if(!sendMailCompleted)
            SubmitMessage.Text += "<br/>Lỗi gởi thư!/Error send mail!";
        }

        protected void Approved_Click(object sender, EventArgs e)
        {
            SaveLineManagerComment(true);
        }

        protected void Rejected_Click(object sender, EventArgs e)
        {
            SaveLineManagerComment(false);
        }

        private void SaveLineManagerComment(bool approved)
        {
            try
            {
                DateTime date = DateTime.Today;
                //if (ReportDate.SelectedDate.HasValue) date = ReportDate.SelectedDate.Value;
                SPList listDailyReport = CS2Web.GetList(Constants.LIST_DAILY_REPORT);
                SPQuery query = new SPQuery();
                query.Query = String.Format(@"<Where>
                  <And>
                     <Eq>
                        <FieldRef Name='Title' />
                        <Value Type='Text'>{0}</Value>
                     </Eq>
	                <Eq>
		                <FieldRef Name='Author' LookupId='TRUE'/>
		                <Value Type='User'>{1}</Value>
	                </Eq>
                  </And>
               </Where>", date.ToString(Constants.DATE_FORMAT), Data.Reporter.Id);
                SPListItemCollection items = listDailyReport.GetItems(query);
                SPListItem item;
                if (CS2SPQueryBuilder.HasItems(items))
                {
                    item = items[0];
                }
                else
                {
                    item = listDailyReport.AddItem();
                    item["Title"] = date.ToString(Constants.DATE_FORMAT);
                }
                item["LineManagerComment"] = LineManagerComment.Text;
                item["Status"] = approved ? "Approved" : "Rejected";

                item.SystemUpdate();
                ActionMessage.Text = "<br/>Hoàn thành!<br/>Done!";
                //Send Mail
                MailAddressCollection To = new MailAddressCollection();
                MailAddressCollection CC = new MailAddressCollection();
                foreach (PickerEntity entity in EmailTo.ResolvedEntities)
                {
                    SPFieldUserValue fuv = new SPFieldUserValue(CS2Web.CurrentWeb, Convert.ToInt16(entity.EntityData[PeopleEditorEntityDataKeys.UserId]), entity.Description);
                    To.Add(new MailAddress(fuv.User.Email, fuv.User.Name));
                }
                foreach (PickerEntity entity in EmailCC.ResolvedEntities)
                {
                    SPFieldUserValue fuv = new SPFieldUserValue(CS2Web.CurrentWeb, Convert.ToInt16(entity.EntityData[PeopleEditorEntityDataKeys.UserId]), entity.Description);
                    CC.Add(new MailAddress(fuv.User.Email, fuv.User.Name));
                }
                SendMail(To, CC, approved ? "approved" : "rejected");
            }
            catch
            {
                ActionMessage.Text = "<br/>Có lỗi khi lưu ý kiến, vui lòng thử lại!<br/>Problem save comment, please try again!";
            }
        }

        protected void ReportTasksAssigned_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
            {
                GridEditableItem editItem = (GridEditableItem)e.Item;
                bool IsReporter = CS2Convert.ToBoolean((editItem.FindControl("IsReporter") as HiddenField).Value);
                bool IsProjectManager = CS2Convert.ToBoolean((editItem.FindControl("IsProjectManager") as HiddenField).Value);

                (editItem["Delay"].Controls[0] as CheckBox).Enabled = IsReporter;
                (editItem["ReasonDelay"].Controls[0] as TextBox).Enabled = IsReporter;
                (editItem["PercentComplete"].Controls[0] as RadNumericTextBox).Enabled = IsReporter;
                (editItem["FS"].Controls[0] as RadDatePicker).Enabled = IsReporter;
                (editItem["FF"].Controls[0] as RadDatePicker).Enabled = IsReporter;
                (editItem["AS"].Controls[0] as RadDatePicker).Enabled = IsReporter;
                (editItem["AF"].Controls[0] as RadDatePicker).Enabled = IsReporter;
                (editItem["PMAprovedCheckBox"].Controls[0] as CheckBox).Enabled = IsProjectManager;
                (editItem["PMComment"].Controls[0] as TextBox).Enabled = IsProjectManager;
            } 
        }

        protected void ActionRegister_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }
    }
}
